package com.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.pojo.Cart;
import com.book.pojo.CartVo;
import com.book.pojo.User;
import com.book.pojo.UserCartVo;
import com.book.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

//购物车
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    //加入购物车
    @PostMapping("/add")
    @ResponseBody
    public String add(Integer bookNum, Integer bookId, HttpSession session) {
        //获取用户id
        User user = (User)session.getAttribute("user");
        Integer userId = user.getId();

        //写入参数
        Cart cart = new Cart();
        cart.setCount(bookNum);
        cart.setBookId(bookId);
        cart.setUserId(userId);

        //判断某用户购物车里是否 已经有某本书
        QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
        cartQueryWrapper.eq("user_id", cart.getUserId());
        cartQueryWrapper.eq("book_id", cart.getBookId());
        Cart queryCart = cartService.getOne(cartQueryWrapper); //从数据库中查的结果

        if(queryCart == null){
            //该用户未把某本书加入购物车，保存加入到购物车的信息
            cartService.save(cart);
        }else {
            //更新加入购物车的书 的数量
            queryCart.setCount(queryCart.getCount() + cart.getCount());
            //保存加入到购物车的信息
            cartService.updateById(queryCart);
        }

        return "success";
    }

    //查询当前用户的购物车
    @RequestMapping("/list")
    public String list(HttpSession session, Model model){
        User user = (User)session.getAttribute("user");  //获取用户

        List<CartVo> cartVos = cartService.findCartByUserId(user.getId()); //查询该用户当前购物车物品

        UserCartVo userCartVo = cartService.wrapperCart(cartVos);
        session.setAttribute("userCartInfo", userCartVo); //将用户购物车信息放在session中

        model.addAttribute("cartList", cartVos);
        return "cart";
    }

    //更新购物车信息
    @RequestMapping("/update")
    @ResponseBody
    public String update(HttpSession session, Cart cart){
        //先更新
        cartService.updateById(cart);

        //再查询
        User user = (User)session.getAttribute("user");  //获取用户
        List<CartVo> cartVos = cartService.findCartByUserId(user.getId());

        UserCartVo userCartVo = cartService.wrapperCart(cartVos);
        session.setAttribute("userCartInfo", userCartVo); //将用户购物车信息放在session中

        double price = cartService.getCartIteamTotal(cartVos);
        return String.valueOf(price);
    }

    //删除购物车中的数据
    @PostMapping("/delete")
    @ResponseBody
    public String delete(String ids){
        return cartService.batchDelete(ids);
    }
}
